﻿// Authors: Manuel de la Peña <manuel.delapena@canonical.com>
using DotUpdater.ActivationProcessors;
using Microsoft.ApplicationBlocks.Updater;
using NUnit.Framework;
using Rhino.Mocks;
using System;

namespace DotUpdater.Test.ActivationProcessors
{
    /// <summary>
    /// Unit tests that ensures that the msi commandline processor works as expected.
    /// </summary>
    [TestFixture]
    public class MsiCmdlineProcessorFixture
    {
        #region Variables

        private MsiCmdlineProcessor _msiCmdlineProcessor;
        private MockRepository _mockRepository;
        private IUpdaterTask _task;

        #endregion

        #region Setup & TearDown

        [SetUp]
        public void Setup()
        {
            _mockRepository = new MockRepository();
            _task = _mockRepository.StrictMock<IUpdaterTask>();
            _msiCmdlineProcessor = new MsiCmdlineProcessor();    
        }

        #endregion

        #region Tests

        [Test]
        [ExpectedException(typeof(ApplicationUpdaterException))]
        public void InitThrowToManyFilesException()
        {
            using(_mockRepository.Record())
            {
                Expect.Call(_task.Files)
                    .Return(new[] {"first_file", "second_file"})
                    .Repeat.Once();
            }
            using (_mockRepository.Playback())
            {
                _msiCmdlineProcessor.Init(_task);
            }
        }

        [Test]
        [ExpectedException(typeof(ApplicationUpdaterException))]
        public void InitThrowFileNotFoundException()
        {
            using(_mockRepository.Record())
            {
                Expect.Call(_task.DownloadFilesBase)
                    .Return(Environment.GetFolderPath(Environment.SpecialFolder.Personal))
                    .Repeat.Once();

                Expect.Call(_task.Files)
                    .Return(new[] {"first_file"})
                    .Repeat.Twice();
            }
            using (_mockRepository.Playback())
            {
                _msiCmdlineProcessor.Init(_task);
            }
        }

        #endregion
    }
}
